<script setup>
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { userRegister } from '@/api/user'
import { ElMessage } from "element-plus";

const router = useRouter()

// 注册表单数据
const form = reactive({
  nickname: '',
  phone: '',
  email: '',
  wechatId: '',
  password: '',
  confirmPassword: ''
})
// 错误信息
const errors = reactive({
  nickname: '',
  phone: '',
  email: '',
  wechatId: '',
  password: '',
  confirmPassword: ''
})
// 加载状态
const isLoading = ref(false)
// 密码可见性切换
const showPassword = ref(false)
const showConfirmPassword = ref(false)
// 邮箱正则
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
// 手机号正则（中国大陆）
const phoneRegex = /^1[3-9]\d{9}$/
// 密码强度正则
const passwordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,20}$/

// 切换密码可见性
const togglePasswordVisibility = () => {
  showPassword.value = !showPassword.value
}

const toggleConfirmPasswordVisibility = () => {
  showConfirmPassword.value = !showConfirmPassword.value
}

// 表单提交处理
const handleRegister = () => {
  // 重置错误信息
  Object.keys(errors).forEach(key => (errors[key] = ''))
  // 输入错误处理
  let hasError = false

  // 昵称验证
  if (!form.nickname.trim()) {
    errors.nickname = '请输入用户名'
    hasError = true
  } else if (form.nickname.length < 2 || form.nickname.length > 20) {
    errors.nickname = '用户名长度需在2-20个字符之间'
    hasError = true
  }

  // 手机号验证
  if (!form.phone.trim() || !phoneRegex.test(form.phone)) {
    errors.phone = '请输入有效的手机号'
    hasError = true
  }

  // 邮箱验证
  if (!form.email.trim() || !emailRegex.test(form.email)) {
    errors.email = '请输入有效的邮箱地址'
    hasError = true
  }

  // 密码验证
  if (!form.password.trim()) {
    errors.password = '请输入密码'
    hasError = true
  } else if (!passwordRegex.test(form.password)) {
    errors.password = '密码需包含大小写字母和数字，长度8-20位'
    hasError = true
  }

  // 确认密码验证
  if (!form.confirmPassword.trim()) {
    errors.confirmPassword = '请确认密码'
    hasError = true
  } else if (form.confirmPassword !== form.password) {
    errors.confirmPassword = '两次输入的密码不一致'
    hasError = true
  }

  if (hasError) return

  // 开始加载
  isLoading.value = true

  const query = {
    username: form.nickname,
    phone: form.phone,
    email: form.email,
    wechatId: form.wechatId,
    password: form.password,
  }

  userRegister(query)
      .then(res => {
        if (res.status) {
          router.push('/userLoginPage')
          ElMessage({
            type: 'success',
            message: '注册成功，请登录',
          })
        } else {
          ElMessage({
            type: 'error',
            message: res.message,
          })
        }
      })
      .catch(err => {
        console.error('注册请求失败:', err)
        ElMessage.error('网络错误，请检查网络连接')
      })
      .finally(() => {
        isLoading.value = false
      })
}
</script>

<template>
  <div class="register-container">
    <!-- 背景装饰 -->
    <div class="login-bg">
      <div class="bg-pattern"></div>
      <div class="bg-gradient"></div>
    </div>

    <div class="login-wrapper">
      <!-- 左侧品牌区 -->
      <div class="login-brand">
        <div class="brand-content">
          <div class="logo">
            <!-- 与首页一致的对称式logo：对话气泡+单星星 -->
            <svg
                class="logo-icon"
                viewBox="0 0 48 48"
                fill="none"
            >
              <!-- 外层对称气泡形状 -->
              <path d="M12 24C12 15.1634 19.1634 8 28 8C36.8366 8 44 15.1634 44 24C44 32.8366 36.8366 40 28 40C23.5817 40 19.5291 38.2426 16.5858 35.2993L12 40V24Z"
                    stroke="#FFFFFF" stroke-width="2.5" fill="rgba(255,255,255,0.1)"/>

              <!-- 中央评分星星 -->
              <path d="M24 14L26.5 22L34 22L28 26.5L30 34L24 30L18 34L20 26.5L14 22L21.5 22L24 14Z"
                    fill="#FFFFFF"/>
            </svg>
            <span class="logo-text">点评通</span>
          </div>

          <h3 class="brand-slogan">连接用户与服务</h3>
          <p class="brand-desc">简单注册，畅享更多专属服务与权益</p>

          <!-- 用户端特点 -->
          <div class="brand-features">
            <div class="feature-item">
              <div class="feature-icon">
                <div class="icon-inner">
                  <i class="fa fa-user"></i>
                  <span class="icon-star">★</span>
                </div>
                <div class="icon-pulse"></div>
              </div>
              <div class="feature-text">一键注册，便捷管理个人信息</div>
            </div>
            <div class="feature-item">
              <div class="feature-icon">
                <div class="icon-inner">
                  <i class="fa fa-shield"></i>
                  <span class="icon-star">★</span>
                </div>
                <div class="icon-pulse"></div>
              </div>
              <div class="feature-text">安全保障，保护您的账户安全</div>
            </div>
            <div class="feature-item">
              <div class="feature-icon">
                <div class="icon-inner">
                  <i class="fa fa-bell"></i>
                  <span class="icon-star">★</span>
                </div>
                <div class="icon-pulse"></div>
              </div>
              <div class="feature-text">及时通知，不错过重要信息</div>
            </div>
            <div class="feature-item">
              <div class="feature-icon">
                <div class="icon-inner">
                  <i class="fa fa-gift"></i>
                  <span class="icon-star">★</span>
                </div>
                <div class="icon-pulse"></div>
              </div>
              <div class="feature-text">专属权益，注册即享更多福利</div>
            </div>
          </div>
        </div>
      </div>

      <!-- 右侧注册表单 -->
      <div class="login-form-container">
        <div class="login-form-wrapper">
          <div class="form-header">
            <h2 class="form-title">用户注册</h2>
            <p class="form-subtitle">填写以下信息完成注册，开启您的服务之旅</p>
          </div>

          <form @submit.prevent="handleRegister" class="login-form">
            <!-- 用户名 -->
            <div class="form-group">
              <label for="nickname" class="form-label">
                昵称
                <span class="required-mark">*</span>
              </label>
              <div class="form-input-wrapper">
                <span class="input-icon">
                  <i class="fa fa-user"></i>
                </span>
                <input
                    type="text"
                    id="nickname"
                    v-model="form.nickname"
                    placeholder="请设置2-20个字符的昵称"
                    required
                    :class="{ 'is-error': errors.nickname }"
                    autofocus
                >
              </div>
              <p v-if="errors.nickname" class="error-message">{{ errors.nickname }}</p>
            </div>

            <!-- 手机号 -->
            <div class="form-group">
              <label for="phone" class="form-label">
                手机号
                <span class="required-mark">*</span>
              </label>
              <div class="form-input-wrapper">
                <span class="input-icon">
                  <i class="fa fa-phone"></i>
                </span>
                <input
                    type="text"
                    id="phone"
                    v-model="form.phone"
                    placeholder="请输入11位手机号"
                    required
                    :class="{ 'is-error': errors.phone }"
                >
              </div>
              <p v-if="errors.phone" class="error-message">{{ errors.phone }}</p>
            </div>

            <!-- 邮箱 -->
            <div class="form-group">
              <label for="email" class="form-label">
                电子邮箱
                <span class="required-mark">*</span>
              </label>
              <div class="form-input-wrapper">
                <span class="input-icon">
                  <i class="fa fa-envelope-o"></i>
                </span>
                <input
                    type="email"
                    id="email"
                    v-model="form.email"
                    placeholder="请输入有效的邮箱地址"
                    required
                    :class="{ 'is-error': errors.email }"
                >
              </div>
              <p v-if="errors.email" class="error-message">{{ errors.email }}</p>
            </div>

            <!-- 微信ID -->
            <div class="form-group">
              <label for="wechatId" class="form-label">
                微信号
                <span class="optional-mark">可选</span>
              </label>
              <div class="form-input-wrapper">
                <span class="input-icon">
                  <i class="fa fa-weixin"></i>
                </span>
                <input
                    type="text"
                    id="wechatId"
                    v-model="form.wechatId"
                    placeholder="请输入您的微信号"
                    :class="{ 'is-error': errors.wechatId }"
                >
              </div>
              <p v-if="errors.wechatId" class="error-message">{{ errors.wechatId }}</p>
            </div>

            <!-- 密码 -->
            <div class="form-group">
              <label for="password" class="form-label">
                设置密码
                <span class="required-mark">*</span>
              </label>
              <div class="form-input-wrapper">
                <span class="input-icon">
                  <i class="fa fa-lock"></i>
                </span>
                <input
                    :type="showPassword ? 'text' : 'password'"
                    id="password"
                    v-model="form.password"
                    placeholder="8-20位，包含大小写字母和数字"
                    required
                    :class="{ 'is-error': errors.password }"
                >
                <button
                    type="button"
                    class="toggle-password"
                    @click="togglePasswordVisibility"
                    aria-label="切换密码可见性"
                >
                  <i :class="showPassword ? 'fa fa-eye' : 'fa fa-eye-slash'"></i>
                </button>
              </div>
              <p v-if="errors.password" class="error-message">{{ errors.password }}</p>
            </div>

            <!-- 确认密码 -->
            <div class="form-group">
              <label for="confirmPassword" class="form-label">
                确认密码
                <span class="required-mark">*</span>
              </label>
              <div class="form-input-wrapper">
                <span class="input-icon">
                  <i class="fa fa-lock"></i>
                </span>
                <input
                    :type="showConfirmPassword ? 'text' : 'password'"
                    id="confirmPassword"
                    v-model="form.confirmPassword"
                    placeholder="请再次输入密码"
                    required
                    :class="{ 'is-error': errors.confirmPassword }"
                >
                <button
                    type="button"
                    class="toggle-password"
                    @click="toggleConfirmPasswordVisibility"
                    aria-label="切换密码可见性"
                >
                  <i :class="showConfirmPassword ? 'fa fa-eye' : 'fa fa-eye-slash'"></i>
                </button>
              </div>
              <p v-if="errors.confirmPassword" class="error-message">{{ errors.confirmPassword }}</p>
            </div>

            <button
                type="submit"
                class="login-button"
                :disabled="isLoading"
            >
              <span v-if="!isLoading">提交注册</span>
              <span v-else class="loading-state">
                <i class="fa fa-spinner fa-spin"></i> 注册中...
              </span>
            </button>

            <p class="register-text">
              已有账号？<router-link to="/userLoginPage" class="register-link">立即登录</router-link>
            </p>
          </form>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
// 颜色变量（与商家登录模板保持一致）
$primary: #2563EB;
$primary-light: #3B82F6;
$primary-dark: #1D4ED8;
$primary-glass: rgba(37, 99, 235, 0.1);
$primary-bg: #EFF6FF; // 添加这行定义（浅蓝色背景）
$danger: #FF4D4F;
$danger-glass: rgba(255, 77, 79, 0.1);
$text-primary: #1E293B;
$text-secondary: #64748B;
$text-tertiary: #94A3B8;
$bg-light: #F8FAFC;
$white: #FFFFFF;
$shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
$shadow-hover: 0 8px 30px rgba(0, 0, 0, 0.12);
$transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);


// 基础样式（与商家登录模板保持一致）
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

// 注册容器
.register-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  overflow: hidden;
  padding: 20px;
}

// 背景装饰（与商家登录模板保持一致）
.login-bg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 0;

  .bg-pattern {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-image: url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%232563eb' fill-opacity='0.05'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
  }

  .bg-gradient {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: linear-gradient(135deg, $primary-bg 0%, $white 100%);
  }
}

// 登录包装器（与商家登录模板保持一致）
.login-wrapper {
  display: flex;
  width: 90%;
  max-width: 1200px;
  min-height: 700px;
  background-color: $white;
  border-radius: 24px;
  box-shadow: $shadow-hover;
  position: relative;
  z-index: 1;
  overflow: hidden;

  @media (max-width: 992px) {
    flex-direction: column;
    min-height: auto;
    margin: 20px 0;
  }
}

// 品牌区域（左侧内容适配用户端）
.login-brand {
  flex: 1;
  background: linear-gradient(135deg, $primary 0%, $primary-dark 100%);
  padding: 60px;
  display: flex;
  align-items: center;
  color: $white;
  position: relative;

  &::after {
    content: '';
    position: absolute;
    top: 0;
    right: 0;
    width: 100px;
    height: 100px;
    background: rgba(255, 255, 255, 0.05);
    border-radius: 0 0 0 100px;
  }

  @media (max-width: 992px) {
    padding: 40px;
  }

  .brand-content {
    width: 100%;
    position: relative;
    z-index: 1;

    .logo {
      display: flex;
      align-items: center;
      gap: 14px;
      margin-bottom: 48px;

      .logo-icon {
        width: 52px;
        height: 52px;
        transition: transform 0.3s ease;

        &:hover {
          transform: rotate(5deg) scale(1.05);
        }
      }

      .logo-text {
        font-size: 32px;
        font-weight: 700;
        letter-spacing: -0.5px;
        font-family: 'Segoe UI', system-ui, sans-serif;
      }
    }

    .brand-slogan {
      font-size: 28px;
      font-weight: 600;
      margin-bottom: 16px;
      line-height: 1.3;
    }

    .brand-desc {
      font-size: 17px;
      line-height: 1.7;
      color: rgba(255, 255, 255, 0.85);
      margin-bottom: 60px;
      max-width: 420px;
    }

    .brand-features {
      display: grid;
      grid-template-columns: 1fr 1fr;
      gap: 22px;
      margin-bottom: 60px;

      .feature-item {
        display: flex;
        align-items: center;
        gap: 14px;
        padding: 18px 22px;
        background-color: rgba(255, 255, 255, 0.05);
        border-radius: 12px;
        transition: $transition;

        &:hover {
          background-color: rgba(255, 255, 255, 0.1);
          transform: translateY(-5px) scale(1.02);
          box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
        }

        .feature-icon {
          width: 48px;
          height: 48px;
          background-color: rgba(255, 255, 255, 0.1);
          border-radius: 12px;
          display: flex;
          align-items: center;
          justify-content: center;
          font-size: 20px;
          position: relative;
          z-index: 1;

          .icon-inner {
            position: relative;
            width: 100%;
            height: 100%;
            display: flex;
            align-items: center;
            justify-content: center;

            .icon-star {
              position: absolute;
              color: rgba(255, 255, 255, 0.7);
              font-size: 12px;
              transform: rotate(15deg);
              top: 8px;
              right: 10px;
            }
          }

          .icon-pulse {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(255, 255, 255, 0.2);
            border-radius: 12px;
            transform: scale(0);
            opacity: 0;
            z-index: -1;
            animation: pulse 2.5s infinite;
          }
        }

        .feature-text {
          font-size: 16px;
          color: rgba(255, 255, 255, 0.95);
          font-weight: 500;
          max-width: 200px;
        }
      }
    }
  }
}

// 表单区域（与商家登录模板保持一致）
.login-form-container {
  flex: 1;
  padding: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow-y: auto;

  @media (max-width: 992px) {
    padding: 40px 30px;
    max-height: 80vh;
  }

  .login-form-wrapper {
    width: 100%;
    max-width: 420px;

    .form-header {
      margin-bottom: 48px;
      text-align: center;

      .form-title {
        font-size: 32px;
        font-weight: 700;
        color: $text-primary;
        margin-bottom: 10px;
        position: relative;

        &::after {
          content: '';
          display: block;
          width: 60px;
          height: 4px;
          background: $primary;
          margin: 16px auto 0;
          border-radius: 2px;
        }
      }

      .form-subtitle {
        font-size: 17px;
        color: $text-secondary;
        max-width: 320px;
        margin: 0 auto;
      }
    }
  }
}

// 表单样式（与商家登录模板保持一致）
.login-form {
  .form-group {
    margin-bottom: 20px;

    .form-label {
      display: block;
      font-size: 15px;
      font-weight: 500;
      color: $text-primary;
      margin-bottom: 8px;
      display: flex;
      align-items: center;
      gap: 4px;
    }

    .required-mark {
      color: $danger;
      font-size: 16px;
    }

    .optional-mark {
      color: $text-tertiary;
      font-size: 13px;
      font-weight: normal;
    }

    .form-input-wrapper {
      position: relative;

      .input-icon {
        position: absolute;
        left: 16px;
        top: 50%;
        transform: translateY(-50%);
        color: $text-tertiary;
        font-size: 18px;
        transition: $transition;
      }

      input {
        width: 100%;
        padding: 12px 16px 12px 50px;
        border: 1px solid #E2E8F0;
        border-radius: 12px;
        font-size: 16px;
        color: $text-primary;
        transition: $transition;
        background-color: $bg-light;
        height: 48px;

        &:focus {
          outline: none;
          border-color: $primary;
          box-shadow: 0 0 0 4px $primary-glass;
          background-color: $white;

          + .input-icon {
            color: $primary;
          }
        }

        &::placeholder {
          color: $text-tertiary;
        }

        &.is-error {
          border-color: $danger;
          box-shadow: 0 0 0 4px $danger-glass;
        }
      }

      .toggle-password {
        position: absolute;
        right: 14px;
        top: 50%;
        transform: translateY(-50%);
        background: none;
        border: none;
        color: $text-tertiary;
        cursor: pointer;
        transition: $transition;
        width: 32px;
        height: 32px;
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;

        &:hover {
          color: $primary;
          background-color: $primary-glass;
        }
      }
    }

    .error-message {
      margin-top: 6px;
      font-size: 14px;
      color: $danger;
      display: flex;
      align-items: center;
      padding-left: 2px;

      &::before {
        content: '!';
        display: inline-flex;
        align-items: center;
        justify-content: center;
        width: 16px;
        height: 16px;
        background-color: $danger;
        color: white;
        border-radius: 50%;
        font-size: 12px;
        margin-right: 6px;
        font-weight: bold;
      }
    }
  }

  .login-button {
    width: 100%;
    padding: 14px;
    background: linear-gradient(135deg, $primary 0%, $primary-dark 100%);
    color: $white;
    border: none;
    border-radius: 12px;
    font-size: 17px;
    font-weight: 600;
    cursor: pointer;
    transition: $transition;
    box-shadow: 0 4px 15px rgba(37, 99, 235, 0.25);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    margin-top: 10px;
    height: 50px;

    &:hover:not(:disabled) {
      transform: translateY(-3px);
      box-shadow: 0 8px 25px rgba(37, 99, 235, 0.35);
    }

    &:disabled {
      background: #E2E8F0;
      cursor: not-allowed;
      box-shadow: none;
    }

    .loading-state {
      display: flex;
      align-items: center;
      gap: 8px;
    }
  }

  .register-text {
    margin-top: 25px;
    text-align: center;
    font-size: 15px;
    color: $text-secondary;

    .register-link {
      color: $primary;
      font-weight: 600;
      text-decoration: none;
      transition: $transition;

      &:hover {
        color: $primary-dark;
        text-decoration: underline;
      }
    }
  }
}

// 动画效果（与商家登录模板保持一致）
@keyframes pulse {
  0% {
    transform: scale(0);
    opacity: 0.5;
  }
  50% {
    transform: scale(1);
    opacity: 0;
  }
  100% {
    transform: scale(0);
    opacity: 0;
  }
}

// 响应式优化（与商家登录模板保持一致）
@media (max-width: 992px) {
  .login-wrapper {
    flex-direction: column;
  }

  .brand-features {
    grid-template-columns: 1fr !important;
  }

  .login-brand {
    padding: 40px;

    .brand-slogan {
      font-size: 24px;
    }
  }

  .form-header {
    margin-bottom: 36px !important;

    .form-title {
      font-size: 28px;
    }
  }
}

@media (max-width: 576px) {
  .login-wrapper {
    min-height: auto;
    border-radius: 16px;
  }

  .login-brand {
    padding: 30px 20px;

    .logo {
      margin-bottom: 30px;

      .logo-icon {
        width: 44px;
        height: 44px;
      }

      .logo-text {
        font-size: 26px;
      }
    }

    .brand-desc {
      margin-bottom: 40px;
      font-size: 16px;
    }

    .brand-features {
      gap: 16px;

      .feature-item {
        padding: 14px 18px;

        .feature-icon {
          width: 36px;
          height: 36px;
          font-size: 18px;
        }

        .feature-text {
          font-size: 15px;
        }
      }
    }
  }

  .login-form-container {
    padding: 30px 20px;
  }

  .form-group {
    margin-bottom: 18px !important;
  }

  input {
    padding: 12px 12px 12px 46px !important;
    font-size: 15px !important;
    height: 46px !important;
  }

  .login-button {
    padding: 12px !important;
    font-size: 16px !important;
    height: 48px !important;
  }
}
</style>